<template>
  <div class="app-container">
    <el-form ref="role" :model="role" :rules="validateRules" label-width="120px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="role.roleName"/>
      </el-form-item>
    <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="addOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  //引入banner.js文件
  import role from'@/api/role/role'
  export default {
    //写核心代码
   data() {//定义当前页面变量及设定初始值
     return {
       role:{},
       saveBtnDisabled: false, // 保存按钮是否禁用,
       validateRules: {
         roleName: [{ required: true, trigger: 'blur', message: '角色名必须输入' }]
       }
     }
    },

    created() {
      if(this.$route.params && this.$route.params.id){
       //修改操作
       this.getRoleById(this.$route.params.id)
       }
    },
    methods:{
     addOrUpdate(){
       if(this.$route.params && this.$route.params.id){
        //修改操作
        this.updateRole()
        }else{
          this.addRole()
        }
     },
      //添加角色
    addRole(){//默认page为1
      role.addRole(this.role)
      .then(response=>{
        this.$message({
          type: 'success',
          message: "添加成功"
         })
         this.$router.push({ path: '/role/list' })
      })
    },
    //修改角色
    updateRole(){
      role.updateRole(this.role)
      .then(response=>{
        this.$message({
          type: 'success',
          message: "修改成功"
         })
         this.$router.push({ path: '/role/list' })
      })
    },
    //根据角色Id获取角色信息
    getRoleById(roleId){
      role.getRoleById(roleId)
      .then(response=>{
        this.role=response.data.role
      })
    }
   }
  }
</script>

<style>
</style>
